package com.zephyr.vchcenter.dwrAction;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.zephyr.vchcenter.bean.PbcData;
import com.zephyr.vchcenter.bean.PbcOriginalData;
import com.zephyr.vchcenter.bean.Voucher;
import com.zephyr.vchcenter.bpm.util.RandomUtil;
import com.zephyr.vchcenter.dao.IBaseDao;
import com.zephyr.vchcenter.helper.HibernateSessionFactory;
import com.zephyr.vchcenter.helper.SpringHelper;
import com.zephyr.vchcenter.pbcFile.configBean.Dict;
import com.zephyr.vchcenter.pbcFile.service.IPbcFileParseService;
import com.zephyr.vchcenter.pbcFile.service.impl.PbcFileParseServiceImpl;

/**
 * @author zhaoyunxiao 对人行数据进行操作的action
 */
public class OperatePbcFileDwrAction {
	private final Logger log = Logger.getLogger(OperatePbcFileDwrAction.class);

	public List<PbcOriginalData> listOrigianlPbcDataByProperties(
			PbcOriginalData pbcOriginalData) {
		IBaseDao dao = (IBaseDao) SpringHelper.getBean("baseDao");
		List<PbcOriginalData> list = dao.findByExample(pbcOriginalData);
		return list;
	}

	public PbcOriginalData getPbcOriginalDataInfobyId(String id) {
		IBaseDao dao = (IBaseDao) SpringHelper.getBean("baseDao");
		PbcOriginalData pbcOriginalData = (PbcOriginalData) dao.findById(
				"PbcOriginalData", id);
		// log.debug(pbcOriginalData.toString());
		return pbcOriginalData;
	}

	public Dict getDictBySubno(String subno) {
		IPbcFileParseService pbcFileParseService = new PbcFileParseServiceImpl();
		Dict dict = pbcFileParseService.getDictInfoBySubno(subno);
		return dict;
	}

	public void updateOriginalPbcData(PbcOriginalData originalPbcData) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tr = session.beginTransaction();
		session.update(originalPbcData);
		tr.commit();
		// IBaseDao dao=(IBaseDao)SpringHelper.getBean("baseDao");
		// dao.saveOrUpdate(originalPbcData);
	}

	public int getOriginalPbcFileCountByProp(PbcOriginalData pbcOriginalData) {
		IBaseDao dao = (IBaseDao) SpringHelper.getBean("baseDao");
		List<PbcOriginalData> list = dao.findByExample(pbcOriginalData);
		return list.size();
	}

	public Map<String, String> pretreatPbcFile(Integer state) {
		PbcOriginalData pbcOriginalDataTemp = new PbcOriginalData();
		pbcOriginalDataTemp.setState(state);
		IBaseDao dao = (IBaseDao) SpringHelper.getBean("baseDao");
		List<PbcOriginalData> list = dao.findByExample(pbcOriginalDataTemp);
		log.debug(list.size());
		RandomUtil randomUtil = new RandomUtil();
		Session session = HibernateSessionFactory.getSession();
		// IBaseDao dao=(IBaseDao)SpringHelper.getBean("baseDao");
		Transaction transaction = session.beginTransaction();
		for (PbcOriginalData pbcOriginalData : list) {
			//log.debug("save");
			PbcData pbcData = new PbcData();
			pbcData.setPbcsid(randomUtil.getUUID());
			if (pbcOriginalData.getAccno() != null
					&& pbcOriginalData.getAccno().length() > 0) {
				//log.debug("---");
				pbcData.setAccno(pbcOriginalData.getAccno());
			}
			if (pbcOriginalData.getAmount() != null
					&& pbcOriginalData.getAmount().length() > 0) {
				//log.debug("---");
				pbcData.setAmount(pbcOriginalData.getAmount());
			}
			if (pbcOriginalData.getBrbank() != null
					&& pbcOriginalData.getBrbank().length() > 0) {
				//log.debug("---");
				pbcData.setBrbank(pbcOriginalData.getBrbank());
			}
			if (pbcOriginalData.getExchno() != null
					&& pbcOriginalData.getExchno().length() > 0) {
				//log.debug("---");
				pbcData.setExchno(pbcOriginalData.getExchno());
			}
			if (pbcOriginalData.getImgid() != null
					&& pbcOriginalData.getImgid().length() > 0) {
				pbcData.setImgid(pbcOriginalData.getImgid());
			}
			if (pbcOriginalData.getSubno() != null
					&& pbcOriginalData.getSubno().length() > 0) {
				 pbcData.setSubno(pbcOriginalData.getSubno());
			}
			if (pbcOriginalData.getVchdate() != null
					&& pbcOriginalData.getVchdate().length() > 0) {
				pbcData.setVchdate(pbcOriginalData.getVchdate());
			}
			if (pbcOriginalData.getVchno() != null
					&& pbcOriginalData.getVchno().length() > 0) {
				pbcData.setVchno(pbcOriginalData.getVchno());
			}
			if (pbcOriginalData.getVchpwd() != null
					&& pbcOriginalData.getVchpwd().length() > 0) {
				pbcData.setVchpwd(pbcOriginalData.getVchpwd());
			}
			if (pbcOriginalData.getVchtype() != null
					&& pbcOriginalData.getVchtype().length() > 0) {
				pbcData.setVchtype(pbcOriginalData.getVchtype());
			}
			if (pbcOriginalData.getVcsession() != null
					&& pbcOriginalData.getVcsession().length() > 0) {
				pbcData.setVcsession(pbcOriginalData.getVcsession());
			}
			if (pbcOriginalData.getVsbank() != null
					&& pbcOriginalData.getVsbank().length() > 0) {
				pbcData.setVsbank(pbcOriginalData.getVsbank());
			}
			if (pbcOriginalData.getPbcsid() != null
					&& pbcOriginalData.getPbcsid().length() > 0) {
				pbcData.setOriginalpbcsid(pbcOriginalData.getPbcsid());
			}
			pbcData.setState("1");
			pbcOriginalData.setState(3);
			try {
				session.save(pbcData);
				dao.saveOrUpdate(pbcOriginalData);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		transaction.commit();
		return null;
	}
	public List<PbcData> getPbcDataByProperties(PbcData pbcData){
		IBaseDao dao = (IBaseDao) SpringHelper.getBean("baseDao");
		List<PbcData> list=dao.findByExample(pbcData);
		log.debug(list==null);
		return list;
	}
	public void merge(String voucherId,String pbcsid){
		IBaseDao dao = (IBaseDao) SpringHelper.getBean("baseDao");
		Voucher voucher=(Voucher)dao.findById("Voucher", voucherId);
		voucher.setPbcsid(pbcsid);
		dao.saveOrUpdate(voucher);
		PbcData pbcData=(PbcData)dao.findById("PbcData", pbcsid);
		pbcData.setState("2");
		dao.saveOrUpdate(pbcData);
	}
	
}
